package ticket.model;

import com.xr.util.Assert;
import com.xr.util.DateUtil;
import com.xr.util.MultimapUtil;
import ticket.TicketUtil;
import ticket.enums.SeatType;

import java.util.ArrayList;
import java.util.List;

/**
 * 车次查询条件
 *
 * Created by forvoyager@outlook.com on 2018-09-30 13:50.
 */
public class QueryItem {

  /**
   * 出发日期
   * 默认当天
   * 时间格式：yyyy-MM-dd
   */
  private String departDate;

  /**
   * 发车时间信息
   * 默认24小时范围内皆可
   * 时间段内出发的车次
   * 时间格式：HH:mm
   */
  private String from_time_start;
  private String from_time_end;

  /**
   * 到达时间信息
   * 默认24小时范围内皆可
   * 时间段内到达的车次
   * 时间格式：HH:mm
   */
  private String arrive_time_start;
  private String arrive_time_end;

  /**
   * 站点信息
   * 出发站
   * 到达站
   */
  private String from_station_name;
  private String to_station_name;
  private String from_station_code;
  private String to_station_code;

  /**
   * 车次信息
   * 默认所有列车皆可
   * 只过滤出需要的车次
   */
  private List<String> trainList = new ArrayList<>();

  /**
   * 需要过滤的座位信息
   * 多种座位类型时 |
   * @see ticket.enums.SeatType
   */
  private int filterSeat = 0;

  /**
   * 余票提醒邮件列表
   */
  private List<String> reminderEmails = new ArrayList<>();

  public static class Builder {

    private QueryItem qi;

    public Builder(){
      qi = new QueryItem();
      qi.departDate = DateUtil.parseTimestamp(DateUtil.currentTimeInSecond(), "yyyy-MM-dd"); // 默认当前日期
    }

    public Builder setDepartDate(String departDate) {
      Assert.hasLength(departDate, "出发日期不合法");
      qi.departDate = departDate;
      return this;
    }

    public Builder setFrom_station_name(String from_station_name) {
      Assert.hasLength(from_station_name, "哪里上车？");
      StationInfo stationInfo = MultimapUtil.getOne(from_station_name);
      Assert.notNull(stationInfo, "哪里上车？");
      qi.from_station_code = stationInfo.getCode();
      qi.from_station_name = from_station_name;
      return this;
    }

    public Builder setTo_station_name(String to_station_name) {
      Assert.hasLength(to_station_name, "哪里下车？");
      StationInfo stationInfo = MultimapUtil.getOne(to_station_name);
      Assert.notNull(stationInfo, "哪里上车？");
      qi.to_station_code = stationInfo.getCode();
      qi.to_station_name = to_station_name;
      return this;
    }

    public Builder setFrom_time_start(String from_time_start) {
      Assert.hasLength(from_time_start, "时间不合法");
      qi.from_time_start = TicketUtil.fillAllTimeFormat(qi.departDate, from_time_start);
      return this;
    }

    public Builder setFrom_time_end(String from_time_end) {
      Assert.hasLength(from_time_end, "时间不合法");
      qi.from_time_end = TicketUtil.fillAllTimeFormat(qi.departDate, from_time_end);
      return this;
    }

    public Builder setArrive_time_start(String arrive_time_start) {
      Assert.hasLength(arrive_time_start, "时间不合法");
      qi.arrive_time_start = TicketUtil.fillAllTimeFormat(qi.departDate, arrive_time_start);
      return this;
    }

    public Builder setArrive_time_end(String arrive_time_end) {
      Assert.hasLength(arrive_time_end, "时间不合法");
      qi.arrive_time_end = TicketUtil.fillAllTimeFormat(qi.departDate, arrive_time_end);
      return this;
    }

    public Builder addTrain(String train) {
      qi.trainList.add(train);
      return this;
    }

    public Builder addFilterSeat(SeatType type){
      if(type != null){
        qi.filterSeat = qi.filterSeat | type.getCode();
      }
      return this;
    }

    public Builder addReminderEmail(String email) {
      if(email !=null && email.length() > 0){
        qi.reminderEmails.add(email);
      }
      return this;
    }

    public QueryItem build() {return qi;}
  }

  public String getDepartDate() {
    return departDate;
  }

  public String getFrom_time_start() {
    return from_time_start;
  }

  public String getFrom_time_end() {
    return from_time_end;
  }

  public String getArrive_time_start() {
    return arrive_time_start;
  }

  public String getArrive_time_end() {
    return arrive_time_end;
  }

  public String getFrom_station_name() {
    return from_station_name;
  }

  public String getTo_station_name() {
    return to_station_name;
  }

  public String getFrom_station_code() {
    return from_station_code;
  }

  public String getTo_station_code() {
    return to_station_code;
  }

  public List<String> getTrainList() {
    return trainList;
  }

  public int getFilterSeat() {
    return filterSeat;
  }

  public List<String> getReminderEmails(){return reminderEmails;}
}
